package com.woniu.mygitte.controller;


import com.woniu.mygitte.entity.User;
import com.woniu.mygitte.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/user")
public class UserController {

    @Resource
    UserService userService;


    @RequestMapping("/login")
    @ResponseBody
    public Map<String, Object> login(@RequestBody User user){
        System.out.println("前端接收的参数对象：" + user.getName());
        System.out.println(userService.findUserByName(user.getName()));
        User tmp = userService.findUserByName(user.getName());
        Map<String, Object> response = new HashMap<>();
        if (tmp != null) {
            if (tmp.getPassword().equals(user.getPassword())){
                System.out.println("登录成功！");
                response.put("status", "success");
                response.put("userId", tmp.getId());
                response.put("userName", tmp.getName());
                return response;
            }else {
                response.put("status", "2");
                return response;
            }

        }
        response.put("status", "0");
        return response;
    }

    @RequestMapping("/add")
    @ResponseBody
    public String add(@RequestBody User user){
        User userName = userService.findUserName(user.getName());
        if(userName == null){
            Integer i = userService.inSertUser(user);
            if(i > 0){
                return "1";
            }
        }
        return "0";
    }

}
